let db = require("../db/index");
function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的，所以需要加1
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}`;
}
// 查询所有投诉建议
exports.all = (req, res) => {

    var sql =
        'select complaints.* ,Owner ,Phone,BuildingName,FloorNumber,UnitName,RoomNumber from complaints,room,building, unit,floor where building.BuildingID=unit.BuildingID AND unit.UnitID=floor.UnitID and floor.FloorID=room.FloorID and room.RoomID=complaints.RoomID ';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//修改处理状态
exports.updateState = (req, res) => {
    const currentDate = new Date();
    const now = formatDate(currentDate)
    console.log(req.body);
    const {id,state}=req.body
    var sql =
       `update complaints set ComplaintsState='${state}',DisposeDate='${now}' where ID=${id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};